const sqlOptions = require("../../sql");
const product = require("express").Router()

product.post("/list", async (req, res) => {
   try {
       const {search} = req.body
       if (search !== undefined || search !== null) {
           const { category_id } = req.body
           if (category_id) {
               const list = await req.db.query(sqlOptions({
                   name: "sms_product",
                   method: "list",
                   data: {
                       product_name: search,
                       category_id
                   },
                   key: "product_name",
                   like: true,
                   if: "OR"
               }))
               return res.status(200).send({
                   code: 200,
                   message: "",
                   data: list
               })
           }
           const list = await req.db.query(sqlOptions({
               name: "sms_product",
               method: "list",
               data: {
                   product_name: search
               },
               key: "product_name",
               like: true
           }))
           return res.status(200).send({
               code: 200,
               message: "",
               data: list
           })
       }
   } catch (e) {
       return res.status(500).send({
           code: 500,
           message: "系统错误",
           data: ""
       })
   }
})
// 折扣
product.get("/discount", async (req, res) => {
    try {
        const list = await req.db.query(`SELECT * FROM sms_product WHERE discount > 0`)
        return res.status(200).send({
            code: 200,
            message: "",
            data: list
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})
product.post("/config", async (req, res) => {
    try {
        console.log(req.body)
        await req.db.query(sqlOptions({
            name: "oms_order",
            method: "update",
            data: {
                order_status: "6"
            },
            key: {
                id: req.body.order_id
            }
        }))
        const logistics = req.db.query(sqlOptions({
            name: "oms_order_logistics",
            method: "list",
            data: {
                order_id: req.body.order_id
            }
        }))
        if (logistics.length) {
            // 物流状态当前id删除
            await req.db.query(sqlOptions({
                name: "oms_order_logistics",
                method: "delete",
                data: {
                    order_id: req.body.order_id
                }
            }))
        }
        const products = await req.db.query(sqlOptions({
            name: "sms_product",
            data: {
                product_id: req.body.product_id
            },
            method: "list"
        }))
        if (products.length) {
            // const product = products[0]
            await req.db.query(`UPDATE sms_product SET  product_count = product_count + ${req.body.order_count} WHERE product_id = '${req.body.product_id}'`)
            await req.db.query(`UPDATE sms_product_color SET  product_color_count = product_color_count + ${req.body.order_count} WHERE product_id = '${req.body.product_id}'`)

        }
        return res.status(200).send({
            code: 200,
            message: "申请退货成功",
            data: ''
        })
    } catch (e) {
        console.log(e)
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})

module.exports = product
